<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:include="fragments/easyui_insdep_head :: easyui_insdep_head" th:with="title='出货'">
</head>
<link th:href="@{/filter/Fliter.css}" rel="stylesheet"/>
<script type="text/javascript" th:src="@{/filter/Filter.js}"/>
<body>

<div class="easyui-layout" style="width:100%;height:100%;overflow: hidden">
    <div data-options="region:'west',collapsible:false,split:true" style="width:50%;overflow: hidden">
        <table id="exporttt" style="width: 100%;height:100%">
            <script type="text/javascript">
                //<![CDATA[
                $(function () {
                    $('#exporttt').treegrid({
                        idField: 'stockid',
                        treeField: 'warename',
                        pagination: true,
                        columns: [[
                            {title: '名称', field: 'warename', width: "200"},
                            {title: '仓库', field: 'warehousename', width: "50", align: 'right'},
                            {
                                title: '数量', field: 'exportitemnum', width: "50 ", align: 'right',
                                editor: {
                                    type: 'numberspinner',
                                    options: {
                                        min: 1,
                                        required: true
                                    }
                                }
                            }, {
                                field: 'exportchannelid', hidden: true
                            },{
                                field: 'exportchannelname',
                                title: "出货对象",
                                width: "100",
                                align: 'right',
                                editor: {
                                    type: 'combobox',
                                    options: {
                                        valueField: 'exportchannelname',
                                        textField: 'exportchannelname',
                                        method: 'post',
                                        hasDownArrow: true,
                                        url: '/exportChannel/getExportChannelList',
                                        editable: false,
                                        onSelect: function (row) {
                                            $("#exporttt").treegrid('find', $(this).combobox('options').fatherid)['exportchannelid'] = row.exportchannelid;
                                        }
                                    }
                                }
                            },
                            {
                                field: 'exportitemprice', title: '价格', width: '15%', align: 'right',
                                formatter: function (value, row, index) {
                                    if (!value) {
                                        return;
                                    }

                                    var endIndex = value.length;

                                    while (value.charAt(endIndex - 1) == 0) {
                                        endIndex--;
                                    }

                                    if (value.charAt(endIndex - 1) == '.') {
                                        endIndex--;
                                    }
                                    return "￥" + value.substring(0, endIndex);
                                },
                                editor: {
                                    type: 'numberbox', options: {
                                        precision: "8",
                                        formatter: function (value) {
                                            if (!value) {
                                                return;
                                            }

                                            var endIndex = value.length;

                                            while (value.charAt(endIndex - 1) == 0) {
                                                endIndex--;
                                            }

                                            if (value.charAt(endIndex - 1) == '.') {
                                                endIndex--;
                                            }
                                            return value.substring(0, endIndex);
                                        }
                                    }
                                }
                            }
                        ]],
                        onDblClickRow: function (row) {
                            if ($('#exporttt').treegrid('getLevel', row.stockid) == 1) {
                                return;
                            }

                            $('#exporttt').treegrid('beginEdit', row.stockid);
                        },
                        onClickRow:function (row) {
                            if(row.exportitemprice) {
                                totalout -= parseFloat(row.exportitemprice);
                            }

                            $('#exporttt').treegrid('endEdit', row.stockid);

                            if(row.exportitemprice) {
                                totalout += parseFloat(row.exportitemprice);
                            }

                            updateTotal();
                        },
                        onBeginEdit: function (row) {
                            $(this).treegrid('getEditor',{
                                id : row.stockid,
                                field : "exportchannelname"
                            }).target.combobox('options').fatherid = row.stockid;
                        }
                    });

                    updateTotal();
                });

                var totalout = 0;
                
                function updateTotal() {
                    var pager = $('#exporttt').datagrid().datagrid('getPager');	// get the pager of datagrid
                    pager.pagination({
                        layout:[],
                        total : totalout,
                        displayMsg : "总价 {total} 元",
                        buttons: [{
                            iconCls: 'icon-remove',
                            handler: function () {
                                row = $("#exporttt").treegrid('getSelected');

                                if ($('#exporttt').treegrid('getLevel', row.stockid) == 1) {
                                    return;
                                }

                                if (row == null) {
                                    $.messager.alert('警告', '请选中需要删除的项目', 'info');
                                    return;
                                }

                                $.messager.confirm('警告', '确定删除该项目吗', function (r) {
                                    totalout -= parseFloat(row.exportitemprice);

                                    // 先获取父节点后移除该节点
                                    var parent = $('#exporttt').treegrid('getParent', row.stockid);
                                    $('#exporttt').treegrid('remove', row.stockid);

                                    // 如果父节点没有孩子，移除父节点
                                    if ($('#exporttt').treegrid('getChildren', parent.stockid).length == 0) {
                                        $('#exporttt').treegrid('remove', parent.stockid);
                                    }

                                    updateTotal();
                                });
                            }
                        },{
                            iconCls: 'icon-save',
                            handler: function () {
                                var jsonstr="[]";
                                var jsonarray = eval('('+jsonstr+')');

                                $.each($('#exporttt').treegrid('getRoots'), function (index, item) {
                                    $.each($('#exporttt').treegrid('getChildren', item.stockid), function (index, row) {
                                        var arr  =
                                            {
                                                "wareid" : row.wareid,
                                                "warehouseid" : row.warehouseid,
                                                "exportchannelid" : row.exportchannelid,
                                                "exportitemnum" : row.exportitemnum,
                                                "exportitemprice" : row.exportitemprice,
                                            }
                                        jsonarray.push(arr);
                                    });
                                });

                                $.ajax({
                                    url: "/export/exportWares",
                                    type: "POST",
                                    datType: "JSON",
                                    contentType: "application/json;charset=UTF-8",
                                    data: JSON.stringify(jsonarray),
                                    success: function (data) {
                                        var data = data == "success" ? "出货成功" : "出货失败";
                                        $.messager.alert('结果', data, 'info');
                                        $("#exporttt").treegrid('reload');
                                        $("#tt").datagrid('reload');
                                    }
                                });
                            }
                        },]
                    });
                }
                //]]>
            </script>
        </table>
    </div>
    <div data-options="region:'center',collapsible:false" style="overflow: hidden">
        <table id="tt" class="easyui-datagrid" style="width:100%;height:100%"
               toolbar="#ttb" pageList='[1,10,20,30,50]' singleSelect="true"
               pagination="true">
            <thead>
            <tr>
                <th field="barcode" style="width: 15%">条形码</th>
                <th field="warename" style="width: 40%">名称</th>
                <th field="waredescribe" style="width: 15%">描述</th>
                <th field="warehousename" style="width: 10%">仓库</th>
                <th field="stocknum" style="width: 5%">库存</th>
            </tr>
            </thead>
            <div id="ttb" style="padding:3px">
                <div id="filter"></div>
                <span>名称:</span>
                <input id="warename" style="line-height:26px;border:1px solid #ccc"/>
                <span>描述:</span>
                <input id="waredescribe" style="line-height:26px;border:1px solid #ccc"/>
                <span>条形码:</span>
                <input id="barcode" style="line-height:26px;border:1px solid #ccc"/>
                <input id="componentid" type="hidden"/>
                <input id="componentparameterdetailid" type="hidden"/>
                <input type="checkbox" id="isFuzzy"/>模糊
                <a href="#" class="easyui-linkbutton" plain="true" onclick="doSearch()">查找</a>
                <a href="#" class="easyui-linkbutton" plain="true" onclick="doReSet()">重置</a>

                <script type="text/javascript">
                    //<![CDATA[
                    $(function () {
                        //单选
                        $('#filter').comboboxfilter({
                            url: '/component/getFilterComponent',
                            text: "配件",
                            onSelect: function (itemData) {
                                $("#componentid").val(itemData.id);
                                doSearch();
                                // 获取他的属性
                                $.post("/component/parameter/getComponentParameter", {
                                    componentid: itemData.id
                                }, function (data) {
                                    if (typeof (data) == typeof ("string")) {
                                        data = $.parseJSON(data);
                                    }
                                    // 遍历属性
                                    $.each(data, function (i, item) {
                                        // 新建一个div
                                        var id = "filter" + i;
                                        $('#filter').after("<div id = '" + id + "' name='Dfilter'/ >")
                                        // 为该div初始化值
                                        id = '#' + id;
                                        $(id).comboboxfilter({
                                            url: '/component/parameter/detail/getFilterComponentParameterDetail',
                                            text: item.componentparametername,
                                            param: {
                                                componentparameterid: item.componentparameterid
                                            },
                                            onSelect: function (itemData) {
                                                $("#componentparameterdetailid").val(itemData.id);
                                                doSearch();
                                            },
                                            unSelect: function (itemData) {
                                                $("#componentparameterdetailid").val("");
                                                doSearch();
                                            }
                                        });
                                    });
                                })
                            },
                            unSelect: function (itemData) {
                                $("#componentid").val("");
                                doSearch();
                                $("div[name='Dfilter']").each(function () {
                                    $(this).comboboxfilter('clear');
                                    $(this).remove();
                                });
                            }
                        });

                        $('#tt').datagrid({
                            url: "/stock/getStock",
                            onClickRow: function (index, row) {
                                if ($('#exporttt').treegrid('find', row.stockid)) {
                                    return;
                                }

                                if (!row.componentname) {
                                    row.componentname = "未定义";
                                }

                                // 寻找是否有分组
                                if (!$('#exporttt').treegrid('find', row.componentname)) {
                                    // 没有分组,新建一个分组
                                    $("#exporttt").treegrid('append', {
                                        data: [{
                                            stockid: row.componentname,
                                            warename: row.componentname,
                                        }]
                                    });
                                }

                                $("#exporttt").treegrid('append', {
                                    parent: row.componentname,
                                    data: [{
                                        stockid: row.stockid,
                                        wareid: row.wareid,
                                        warehouseid: row.warehouseid,
                                        warename: row.warename,
                                        warehousename: row.warehousename,
                                        exportitemnum : 1
                                    }]
                                });

                            }
                        });
                    });

                    function doSearch() {
                        $('#tt').datagrid('load', {
                            warename: $('#warename').val(),
                            barcode: $('#barcode').val(),
                            waredescribe: $("#waredescribe").val(),
                            isFuzzy: $('#isFuzzy').is(':checked'),
                            componentid: $('#componentid').val(),
                            componentparameterdetailid: $('#componentparameterdetailid').val(),
                        });
                    }
                    function doReSet() {
                        $('#warename').val("");
                        $('#waredescribe').val("");
                        $('#barcode').val();
                        doSearch();
                    }
                    //]]>
                </script>
            </div>
        </table>
    </div>
</div>

</body>
</html>